package procilege.lang;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class EntityExprOr implements EntityExprElement,
		CompositeExpr<EntityExprElement> {

	private Set<EntityExprElement> terms = new HashSet<EntityExprElement>();

	public Set<EntityExprElement> getTerms() {
		return terms;
	}

	public void add(EntityExprElement elem) {

		or(elem);
	}

	public Iterator<EntityExprElement> iterator() {

		return terms.iterator();
	}

	public int size() {
		return terms.size();
	}

	public EntityExprOr() {
	}

	public EntityExprOr(Collection<EntityExprElement> terms) {

		for (EntityExprElement t : terms) {
			or(t);
		}
	}

	public void or(EntityExprElement term) {

		if (term == null)
			return;

		if (term instanceof EntityExprOr) {
			for (EntityExprElement b : ((EntityExprOr) term).getTerms()) {
				terms.add(b);
			}
		} else {
			terms.add(term);
		}
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((terms == null) ? 0 : terms.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final EntityExprOr other = (EntityExprOr) obj;
		if (terms == null) {
			if (other.terms != null)
				return false;
		} else if (!terms.equals(other.terms))
			return false;
		return true;
	}
}